home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Tools - Objects / MacApp / MacApp 3.0a2 / CPlusIncludes / Geometry.h < prev    next >
Text File  |  1991-05-01  |  8KB  |  297 lines

  1. // Geometry.h
  2. // Copyright 1984-1991 Apple Computer, Inc. All rights reserved.
  3.  
  4. #ifndef __GEOMETRY__
  5. #define __GEOMETRY__
  6.  
  7. #ifndef __TYPES__
  8. #include <Types.h>
  9. #endif
  10.  
  11. //--------------------------------------------------------------------------------
  12. // Selector operator indicator enumeration. Which coordinate do we want
  13. // to select?
  14. //--------------------------------------------------------------------------------
  15.     
  16. enum VHSelect {    vSel, hSel };
  17.  
  18.  
  19. //--------------------------------------------------------------------------------
  20. // SimplePoint: is used as the field type for some structs in the toolbox that
  21. // we don't want promoted to a class; either because of constructor problems or
  22. // the struct may appear in a union. Simple point is also used as the base class
  23. // for Point; we simply inherit the data members.
  24. //--------------------------------------------------------------------------------
  25.  
  26. struct SimplePoint
  27. {
  28.     short v;
  29.     short h;
  30. };
  31.  
  32.  
  33. //--------------------------------------------------------------------------------
  34. // Point: is a toolbox compatible class for the old struct Point. It is bitwise
  35. // compatible with the old struct because it contains no virtual functions.
  36. //--------------------------------------------------------------------------------
  37.  
  38. typedef struct Point *PointPtr;
  39.  
  40. struct Point : public SimplePoint
  41. {
  42.     // Constructors
  43.  
  44.     Point();                                    // Default constructor.
  45.     Point(short iV,
  46.           short iH);
  47.     Point(const SimplePoint& pt);
  48.     
  49.     // Copy simply returns a copy of the object. Useful for creating automatic copies
  50.     // of an object on the stack, in a function call.
  51.     
  52.     Point Copy() const;
  53.     
  54.     // Conversion operator, for converting Point to a textual representation of a
  55.     // Point. "Point(v,h)".
  56.     
  57.     operator char*() const;
  58.  
  59.     // Selector operators, two are needed one for operating on const Points
  60.     // and the other on non-const Points.
  61.     
  62.     short& operator[](VHSelect sel);            // For non-const Point
  63.     const short& operator[](VHSelect sel) const;// For const Point
  64.  
  65.     // Arithmetic operators
  66.  
  67.     Point operator+(const Point& pt) const;        // c = a + b    (a,b and c are Points)
  68.     Point operator-(const Point& pt) const;        // c = a - b    (a,b and c are Points)
  69.  
  70.     Point& operator+=(const Point& pt);            // c += a        (a and c are points, and
  71.                                                 //                a reference to a is returned,
  72.                                                 //                so these can be strung together)
  73.     Point& operator-=(const Point& pt);            // c -= a        ( ditto )
  74.  
  75.     // Relational operators
  76.  
  77.     Boolean operator!=(const Point& pt) const;    // a != b
  78.     Boolean operator==(const Point& pt) const;    // a == b
  79.     Boolean operator>(const Point& pt) const;    // a > b
  80.     Boolean operator<(const Point& pt) const;    // a < b
  81.     Boolean operator>=(const Point& pt) const;    // a >= b
  82.     Boolean operator<=(const Point& pt) const;    // a <= b
  83. };
  84.  
  85.  
  86. //--------------------------------------------------------------------------------
  87. // Inline method definitions for Point.
  88. //--------------------------------------------------------------------------------
  89.  
  90. inline Point::Point(short iV, short iH)
  91. {
  92.     v = iV;
  93.     h = iH;
  94. }
  95.  
  96. inline Point::Point(const SimplePoint& pt)
  97. {
  98.     v = pt.v;
  99.     h = pt.h;
  100. }
  101.  
  102. inline Point Point::Copy() const
  103. {
  104.     Point pt;
  105.     
  106.     pt.v = v;
  107.     pt.h = h;
  108.     
  109.     return pt;
  110. }
  111.  
  112.  
  113. //--------------------------------------------------------------------------------
  114. // Selector operator indicator enumeration. Which Point do we want
  115. // to select, top-left or bottom-right?
  116. //--------------------------------------------------------------------------------
  117.     
  118. enum PointSelector { topLeft, botRight };
  119.  
  120.  
  121. //--------------------------------------------------------------------------------
  122. // SimpleRect: is used as the field type for some structs in the toolbox that
  123. // we don't want promoted to a class; either because of constructor problems or
  124. // the struct may appear in a union. Simple Rect is also used as the base class
  125. // for Rect; we simply inherit the data members.
  126. //--------------------------------------------------------------------------------
  127.  
  128. struct SimpleRect
  129. {
  130.     short top;
  131.     short left;
  132.     short bottom;
  133.     short right;
  134. };
  135.  
  136.  
  137. //--------------------------------------------------------------------------------
  138. // Rect: is a toolbox compatible class for the old struct Rect. It is bitwise
  139. // compatible with the old struct because it contains no virtual functions.
  140. //--------------------------------------------------------------------------------
  141.  
  142. typedef struct Rect *RectPtr;
  143.  
  144. struct Rect : public SimpleRect
  145. {
  146.  
  147.     // Rect constructors
  148.  
  149.     Rect();
  150.     Rect(short iTop,
  151.          short iLeft,
  152.          short iBottom,
  153.          short iRight);
  154.     Rect(const Rect& rect);
  155.     Rect(const Point& topLeftPt,
  156.          const Point& botRightPt);
  157.     Rect(const SimpleRect& rect);
  158.     
  159.     // Copy simply returns a copy of the object. Useful for creating automatic copies
  160.     // of an object on the stack, in a function call.
  161.     
  162.     Rect Copy() const;
  163.     
  164.     // Conversion operator, for converting Rect to a textual representation of a
  165.     // Rect. "Rect(top,left,bottom,right)".
  166.     
  167.     operator char*() const;
  168.  
  169.     // Point selectors for Rect. One for operating on const Rects and one
  170.     // for non-const Rects.
  171.  
  172.     Point& operator[](PointSelector sel);                // Selector for non-const Rects
  173.     const Point& operator[](PointSelector sel) const;    // Selector for const Rects
  174.  
  175.     // Operators for adding and subtracting one Rect from to/from another.
  176.  
  177.     Rect operator+(const Rect& rt) const;
  178.     Rect operator-(const Rect& rt) const;
  179.     Rect& operator+=(const Rect& rt);
  180.     Rect& operator-=(const Rect& rt);
  181.  
  182.     // Operators overloaded for adding and subtracting some increment to/from each point
  183.     // along both axis. Very convenient for translating Rects. These take a point and since
  184.     // Point has a constructor that takes two shorts the Rect windowRect can be translated
  185.     // 100 pixels in the positive y direction by the statement:
  186.     //
  187.     //    windowRect = windowRect + Point (0, 100);    or
  188.     //     windowRect += Point (0, 100);
  189.  
  190.     Rect operator+(const Point& pt) const;
  191.     Rect operator-(const Point& pt) const;
  192.     Rect& operator+=(const Point& pt);
  193.     Rect& operator-=(const Point& pt);
  194.     
  195.     // Inset a Rect by Point.
  196.     
  197.     Rect& Inset(const Point& delta);
  198.  
  199.     // Equality operators, other relational operator could be defined such as <. But their
  200.     // meaning is less clear and probably better implemented as methods. For example,
  201.     // aRect < bRect could return true if aRect was inside of bRect, or could return true
  202.     // if the area of aRect was less than the area of bRect.
  203.  
  204.     Boolean operator==(const Rect& rt) const;
  205.     Boolean operator!=(const Rect& rt) const;
  206.  
  207.     // Two simple area operators, the intersection && (logical and in C++) and the
  208.     // union || (logical or in C++). The definition of union here is to return a Rect
  209.     // that exactly encloses its operands.
  210.  
  211.     Rect operator&&(const Rect&) const;
  212.     Rect operator||(const Rect& rt) const;
  213.  
  214.     // Returns true if a valid rectangle (left < right and top < bottom). If not
  215.     // a valid rectangle then return false and set all coordinates to 0.
  216.     
  217.     Boolean ValidRect();
  218.     
  219.     // Return true if the Rect is empty (right - left = 0 and bottom - top = 0)
  220.     
  221.     Boolean Empty() const;
  222.     
  223.     // Returns the length of a Rect in a given dimension.
  224.     
  225.     short Length(VHSelect sel) const;
  226.     
  227.     // Returns the size of a Rect in both dimensions. (a Point)
  228.     
  229.     Point Size() const;
  230.     
  231.     // The Contains method takes either a Point or a Rect and returns true if the operand
  232.     // is inside of the Rect the method is applied to.
  233.  
  234.     Boolean Contains(const Point& pt) const;
  235.     Boolean Contains(const Rect& rt) const;
  236.  
  237. private:
  238.  
  239.     short Min(const short a,
  240.               const short b) const;
  241.     short Max(const short a,
  242.               const short b) const;
  243. };
  244.  
  245.  
  246. //--------------------------------------------------------------------------------
  247. // Inline definitions for methods in Rect
  248. //--------------------------------------------------------------------------------
  249.  
  250. inline Rect::Rect() { }
  251.  
  252. inline Rect::Rect(short iTop, short iLeft, short iBottom, short iRight)
  253. {
  254.     top = iTop;
  255.     left = iLeft;
  256.     bottom = iBottom;
  257.     right = iRight;
  258. }
  259.  
  260. inline Rect::Rect(const Rect& rect)
  261. {
  262.     top = rect.top;
  263.     left = rect.left;
  264.     bottom = rect.bottom;
  265.     right = rect.right;
  266. }
  267.  
  268. inline Rect::Rect(const Point& topLeftPt, const Point& botRightPt)
  269. {
  270.     top = topLeftPt.v;
  271.     left = topLeftPt.h;
  272.     bottom = botRightPt.v;
  273.     right = botRightPt.h;
  274. }
  275.  
  276. inline Rect::Rect(const SimpleRect& rect)
  277. {
  278.     top = rect.top;
  279.     left = rect.left;
  280.     bottom = rect.bottom;
  281.     right = rect.right;
  282. }
  283.  
  284. inline Rect Rect::Copy() const
  285. {
  286.     Rect rt;
  287.     
  288.     rt.top = top;
  289.     rt.left = left;
  290.     rt.bottom = bottom;
  291.     rt.right = right;
  292.     
  293.     return rt;
  294. }
  295.  
  296.  
  297. #endif